﻿function myTools() {
}
/**
 * 从url中取数据
 */
myTools.getQueryString = function (id) {
    var name, value, i;
    var str = location.href;
    var num = str.indexOf("?");
    str = str.substr(num + 1);
    var arrtmp = str.split("&");
    for (var i = 0; i < arrtmp.length; i++) {
        num = arrtmp[i].indexOf("=");
        if (num > 0) {
            name = arrtmp[i].substring(0, num);
            value = arrtmp[i].substr(num + 1);
            this[name] = value;
        }
    }
    return this[id];
};
myTools.bindForm = function (formName, data) {
    try {
        for (var key in data) {
            var obj = document.getElementById(formName + "_" + key);
            if (obj) {
                if (obj.tagName.toLowerCase() == "input")
                    if (obj.type.toLowerCase() == "checkbox") {
                        var bCheck = data[key];
                        if (bCheck == 1)
                            obj.checked = true;
                    }
                obj.value = data[key];
            }
        }
    } catch (e) {
        alert("客户端绑定错误:\n" + "\nmessage:\n\t" + e.message);
    }

};
/*
 * 窗口组件 -----------------------------------------
 */
var win = (function () {
    return {
        /**
         * 创建窗口
         *
         * @param n:窗口id;
         * @param t:窗口标题头内容;
         * @param p:连接路径;
         * @param w:窗口宽度;
         * @param h:窗口高度;
         * @param c:窗口蒙板;
         * @param i:窗口是否是外部链接;
         * @param s:窗口是否定位;
         * @param l:窗口位置;
         * @param o:窗口父对象;
         * @param q:窗口级别
         *            one 一级 ,two 二级;
         */
        open:function (n, t, p, w, h, c, i, s, l, o, q) {
            lhgdialog.opendlg(n, t, p, w, h, c, i, s, l, o, q);
        },
        /**
         * 创建窗口工具栏上的按钮
         *
         * @param n:按钮id;
         * @param t:按钮标题头内容;
         * @param c:按钮单击事件方法
         */
        crebtn:function (n, t, c) {
            window.parent.crebtn(n, t, c);
        },
        /**
         * 删除窗口工具栏上的按钮
         *
         * @param n:按钮id;
         */
        rembtn:function (n) {
            window.parent.rembtn(n);
        },
        /**
         * 删除窗口标题栏上的按钮
         *
         * @param n:按钮id;
         */
        remTopbtn:function (n) {
            window.parent.remTopbtn(n);
        },
        /**
         * 取得窗口父级对象
         *
         * @param n:窗口id;
         */
        getParent:function (n) {
            return window.parent.parent;
        },
        /**
         * 关闭窗口
         *
         * @param n:窗口id;
         */
        close:function (n) {
            window.parent.cancel(n);
        },
        /**
         * 取得窗口的属性 w 宽 h 高
         *
         * @return wc Object;
         */
        getWinConf:function () {
            var wc = {
                w:window.parent.innerWidth,
                h:window.parent.innerHeight
            };
            return wc;
        }
    };

})();

// ================第一个元素获得焦点==========================
//window.attachEvent('onload', firstElemOnfocus);
if (window.addEventListener) {
    window.addEventListener('DOMContentLoaded', firstElemOnfocus, false);   //firefox
    window.addEventListener('load', firstElemOnfocus, false);
} else if (window.attachEvent) {
    window.attachEvent('onload', firstElemOnfocus);  //IE
}

function firstElemOnfocus() {
    var obj = document.all;
    if (!obj)return;
    var flg = false;
    for (var i = 0; i < obj.length; i++) {
        var el = obj[i];
        if (!el.disabled) {
            switch (el.type) {
                case "text":
                case "password":
                case "textarea":
                    el.focus();
                    flg = true;
                    break;
            }
        }
        if (flg)
            break;
    }
}
function getModuleOperationByUserDMAndModuleDM(user_dm, module_dm, page_jb, viewDIV) {
    var contextPath = contextPath ? contextPath : parent.parent.contextPath;
    var json = {
        url:contextPath + "/server/base/selectAll.do",
        showMsg:false,
        msg:"数据加载中.....",
        params:{
            "USER_DM":user_dm,
            "MODULE_DM":module_dm,
            "PAGE_JB":page_jb,
            "SQLID":"module.selectOpertionBY_USER__AND_MODULE"
        },
        callback:[ function (json) {
            getModuleOperationByUserDMAndModuleDMCallBack(json, viewDIV);
        } ]
    };
    // new Ajax().sendData(json);
    AjaxTools.ajax(json);
}
function getModuleOperationByUserDMAndModuleDMCallBack(json, viewDIV) {
    var html = "";
    for (var i = 0; i < json.data.length; i++) {
        var d = json.data[i];
        html += "<input type=\"button\" class=\"button\" value=\""
            + d.MODULE_NAME + "\" onclick=\"" + d.MODULE_URL + "\">";
    }
    document.getElementById(viewDIV).innerHTML = html;
}
/**
 * 将字符串str1长度不够length时，左侧补充str2
 */
function LPad(str1, str2, length) {
    for (var i = str1.length; i < length; i++) {
        str1 = str2 + "" + str1;
    }
    return str1;
}

getPage = function (curPage, totalPage) {
    if (curPage <= 0) {
        return 0;
    } else if (parseInt(curPage) >= parseInt(totalPage)) {
        return  parseInt(totalPage) - 1;
    } else {
        return curPage;
    }
};

// 获取form表单数据
function getContent(theform) {
    var els = theform.elements;
    var len = els.length;
    var queryString = "";
    this.addField = function (name, value) {
        if (queryString.length > 0) {
            queryString += "&";
        }
        queryString += name + "=" + value;
    };
    for (var i = 0; i < len; i++) {
        var el = els[i];
        if (!el.disabled) {
            switch (el.type) {
                case "text":
                case "password":
                case "hidden":
                case "textarea":
                    this.addField(el.name, el.value);
                    break;
                case "select-one":
                    if (el.selectedIndex >= 0) {
                        this.addField(el.name,
                            el.options[el.selectedIndex].value);
                    }
                    break;
                case "select-multiple":
                    for (var j = 0; j < el.options.length; j++) {
                        if (el.options[j].selected) {
                            this.addField(el.name, el.options[j].value);
                        }
                    }
                    break;
                case "checkbox":
                case "radio":
                    if (el.checked) {
                        this.addField(el.name, el.value);
                    }
                    break;
            }
        }
    }
    return queryString.substring(0, queryString.length);
}
;

//=====================表格处理=======================================//
function myTable() {
}
function add_event(tr) {
    tr.onmouseover = function () {
        tr.className += ' hover';
    };
    tr.onmouseout = function () {
        tr.className = tr.className.replace(' hover', '');
    };
}
function stripe(table) {
    var trs = table.getElementsByTagName("tr");
    for (var i = 1; i < trs.length; i++) {
        var tr = trs[i];
        tr.className = i % 2 != 0 ? 'odd' : 'even';
        add_event(tr);
    }
}
function setTableCss(tableId) {
    var table = document.getElementById(tableId);
    if (table.className == 'datagrid1' || table.className == 'datagrid2'
        || table.className == 'datagrid3' || table.className == 'datagrid4') {
        stripe(table);
    }
}
/**
 *单选 取得选中的记录的id
 */
myTable.getSelectedID = function () {
    var options = document.getElementsByName("options");
    var flg = false;
    var id = "";
    for (var i = 0; i < options.length; i++) {
        if (options[i].checked) {
            if (flg) {
                alert("不能多选！");
                return false;
            }
            id = options[i].value;
            flg = true;
        }

    }
    if (flg) {
        return id;
    } else {
        alert("请选择操作对象！");
        return false;
    }
};
/**
 *取得选中的记录的id 允许多选， 以逗号分隔返回
 */
myTable.getSelectedIDS = function () {
    var options = document.getElementsByName("options");
    var flg = false;
    var id = "";
    for (var i = 0; i < options.length; i++) {
        if (options[i].checked) {
            var val = options[i].value;
            id = id.length > 0 ? (id + "," + val) : val;
            flg = true;
        }

    }
    return id;
};
/**
 *grid 全选
 */
myTable.checkAll = function () {
    var options = document.getElementsByName("options");
    var flag = false;
    for (var i = 0; i < options.length; i++) {
        if (options[i].checked) {
            flag = true;
            break;
        }
    }
    if (flag) {
        for (var i = 0; i < options.length; i++) {
            options[i].checked = false;
        }
    } else {
        for (var i = 0; i < options.length; i++) {
            options[i].checked = true;
        }
    }
};
//=============================================================

function RowSelect(row, name) {
    if (!name) name = "row";
    if (window[name]) {
        window[name].className = window[name].getAttribute("oldClass");
    }
    row.className = "tableRowSelected";
    window[name] = row;
}